//
// $Id$

package com.samskievert.cactusboom;

import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;

import com.threerings.parlor.game.client.GameController;
import com.threerings.toybox.data.ToyBoxGameConfig;
import com.threerings.toybox.util.ToyBoxContext;

import com.threerings.util.Name;
import com.threerings.parlor.turn.client.TurnGameController;
import com.threerings.parlor.turn.client.TurnGameControllerDelegate;

/**
 * Manages the client side mechanics of the game.
 */
public class CBController extends GameController
        implements TurnGameController
{
    public boolean standardView = true;
    
    public CBController ()
     {
         addDelegate(_turndel = new TurnGameControllerDelegate(this));
     }
 
     // from interface TurnGameController
     public void turnDidChange (Name turnHolder)
     {
        
         // if it's our turn, activate piece placement
         System.out.println("Play passes to " + turnHolder);
         _panel.bview.setPlacingMode(_turndel.isOurTurn() ? _color : -1);
         _panel.tview.setPlacingMode(_turndel.isOurTurn() ? _color : -1);
         
         toggleView();
      
     }
     
     public boolean turnCheck(){
         return _turndel.isOurTurn();
     }
     
     public void toggleView(){
        standardView = !standardView;
        _panel.boardContainer.removeAll();
        if(standardView){
            _panel.boardContainer.add(_panel.tview);
        }else{
            _panel.boardContainer.add(_panel.bview); 
            if(turnCheck()) _gameobj.manager.invoke("newBoard",_turndel.getTurnHolderIndex());
            //CBLogic logic = new CBLogic(8);
            //logic.setState(_gameobj); 
            //logic.getDifficulty(_gameobj,_turndel.getTurnHolderIndex());
        }
        _panel.boardContainer.validate();
        _panel.updateUI();
     }

    /**
     * Requests that we leave the game and return to the lobby.
     */
    public void backToLobby ()
    {
        _ctx.getLocationDirector().moveBack();
    }
    
    public void passTurn()
    {
        if(_turndel.isOurTurn()) _gameobj.manager.invoke("passTurn");
    }
    
    public void resetBoard()
    {
        System.out.println("controller command...");
        _gameobj.manager.invoke("resetBoard");
    }
    
    public void rollDice()
    {
        if(_turndel.isOurTurn()) System.out.println("Rollin' rollin' rollin'...");
    }
    
    public void resetPoints()
    {
        _gameobj.manager.invoke("letsBegin");
    }
    
    public void clearBoard()
    {
        _gameobj.manager.invoke("clearBoard");
    }
    
    public void newBoard()
    {
        //_gameobj.manager.invoke("newBoard");
    }

    @Override // from PlaceController
    public void willEnterPlace (PlaceObject plobj)
    {
        super.willEnterPlace(plobj);

        // get a casted reference to our game object
        _gameobj = (CBObject)plobj;
        
        // determine our piece color (-1 if we're not a player)
        _color = _gameobj.getPlayerIndex(((ToyBoxContext)_ctx).getUsername());
 
        // if it's our turn, activate piece placement
        _panel.bview.setPlacingMode(_turndel.isOurTurn() ? _color : -1);
        toggleView();
    }
    
    /** Handles turn-game related stuff. */
    protected TurnGameControllerDelegate _turndel;
 
    /** Our piece color. */
    protected int _color;


    @Override // from PlaceController
    public void didLeavePlace (PlaceObject plobj)
    {
        super.didLeavePlace(plobj);

        // clear out our game object reference
        _gameobj = null;
    }

    @Override // from PlaceController
    protected PlaceView createPlaceView (CrowdContext ctx)
    {
        _panel = new CBPanel((ToyBoxContext)ctx, this);
        return _panel;
    }
    
    @Override //from GameController (methinks)
    protected void didInit ()    {
        //grab the number of spikes from the config file
        super.didInit();
        
       /* ToyBoxGameConfig config = (ToyBoxGameConfig)_config;
        
        System.out.println(_gameobj.NUMSPIKES + " is the current value.");
        String i = (String)config.params.get("Num_Spikes");
        _gameobj.setNUMSPIKES(Integer.valueOf(i).intValue());        */
    }

    @Override // from GameController
    protected void playerReady ()
    {
        super.playerReady();
        _panel.enableCursor();
        
    }
    @Override // from GameController
    protected void gameDidStart ()
    {
        super.gameDidStart();

        // here we can set up anything that should happen at the start of the
        // game

    }

    @Override // from GameController
    protected void gameDidEnd ()
    {
        super.gameDidEnd();
        System.out.println("HEY GAME OVER RECEIVED!");
        _panel.tview.addGameOverSprite();
        // here we can clear out anything that needs to be cleared out at the
        // end of a game
        
       // if we are the winner of the game, display some animated text
       // informing us of this fact
       ToyBoxContext tctx = (ToyBoxContext)_ctx;
       String message = (!_gameobj.isDraw() && _gameobj.isWinner(_color)) ?
           "m.you_win" : "m.game_over";
       _panel.tview.displayFloatingText(tctx.xlate("cactusboom", message));

    }
    
    /**
    * Called when a player requests to play a piece during their turn.
    */
   public void cactusPlaced (CBObject.Cactus cactus)
   {
       if(!_gameobj.localsOnly){
           System.out.println("Cactus placed +" + cactus.x + "+" + cactus.y + ".");
           _gameobj.manager.invoke("placeCactus", cactus);
       }
   }
   
   public void addSpike(CBObject.Cactus cactus)
   {
    if(!_gameobj.localsOnly){
        System.out.println("Spike added to cactus at (" + cactus.x + "," + cactus.y + ")");
        _gameobj.manager.invoke("addSpike", cactus);
    }
    //_gameobj.manager.invoke("checkBoom", cactus);
   }
   
   public void moveCowboy(int whichDir)
   {
    _gameobj.manager.invoke("moveCowboy", whichDir);

    //if we just sent off our last move, toggle the view over
    //if( _gameobj.movePoints[_turndel.getTurnHolderIndex()] == 1) toggleView();
   }
   
   
    /** Our game panel. */
    protected CBPanel _panel;

    /** Our game distributed object. */
    protected CBObject _gameobj;
}
